<?php

namespace App\Models\XS;

use Illuminate\Database\Eloquent\Model;
use App\Models\Common\CollPosition;

class Resume extends Model
{
    protected $table = 'resume';
    public $timestamps = false;
    protected $appends = ['id_sort', 'jobarea', 'jobsort', 'other_important', 'other', 'updatetime_by_day'];

    protected $hidden = ['introduction', 'jobarea1', 'jobarea2', 'jobarea3',
        'jobarea4', 'jobarea5', 'jobsort1', 'jobsort2', 'jobsort3',
        'jobsort4', 'jobsort5', 'resume_work_exp',
        'resume_pro_exp', 'resume_skill', 'resume_edu_exp'];

    public function resume_work_exp()
    {
        return $this->hasMany(ResumeWorkExp::class, 'resume_id')
            ->orderBy('starttime', 'desc')->select(['resume_id', 'post', 'content']);
    }

    public function resume_edu_exp()
    {
        return $this->hasMany(ResumeEducationExp::class, 'resume_id')
            ->orderBy('starttime', 'desc')->select(['resume_id', 'speciality']);
    }

    public function resume_pro_exp()
    {
        return $this->hasMany(ResumeProjectExp::class, 'resume_id')
            ->orderBy('starttime', 'desc')->select(['resume_id', 'post', 'content']);
    }

    public function resume_skill()
    {
        return $this->hasMany(ResumeSkill::class, 'resume_id')
            ->orderBy('updatetime', 'desc')->select(['resume_id', 'skillname']);
    }

    public function getIdSortAttribute()
    {
        return $this->attributes['id'];
    }

    public function getJobareaAttribute()
    {
        return CmsXS::implodeManyCidAndPid($this, 'jobarea');
    }

    public function getJobsortAttribute()
    {
        return CmsXS::implodeCid($this, 'jobsort');
    }

    public function getJobsortNameAttribute()
    {
        $arr = [];
        $p = CollPosition::getOnlyThree();
        for ($i = 1; $i < 6; $i++) {
            $num_key = 'jobsort' . $i;
            $cid = $this->$num_key;
            if ($cid == 0) continue;
            if (array_key_exists($cid, $p)) {
                $arr[] = $p[$cid];
            }
        }
        return implode(' ', $arr);
    }

    public function getBirthdayAttribute($value)
    {
        return (is_null($value) || $value == '0000-00-00') ? 0 : strtotime($value);
    }

    //是否公开头像
    public function getChkphotOpenAttribute($value)
    {
        return ($value == 0 && !empty($this->avatar)) ? 0 : 1;
    }

    public function getUpdatetimeAttribute($value)
    {
        return (is_null($value) || $value == '0000-00-00 00:00:00') ? 0 : strtotime($value);
    }

    public function getUpdatetimeByDayAttribute()
    {
        $value = $this->getOriginal('updatetime');
        if (is_null($value) || $value == '0000-00-00 00:00:00') {
            return 0;
        }
        return strtotime(substr($value, 0, 10) . ' 00:00:00');
    }

    public function getHomeaddressAttribute($value)
    {
        return CmsXS::implodeOneCidAndPid($value);
    }

    public function getCensusAttribute($value)
    {
        return CmsXS::implodeOneCidAndPid($value);
    }

    public function getOtherImportantAttribute()
    {
        $str = '';
        if (!empty($this->jobsort_name)) {
            $str .= $this->jobsort_name . ' ';
        }

        foreach ($this->resume_work_exp as $item) {
            $str .= $item->post . ' ';
        }

        foreach ($this->resume_pro_exp as $item) {
            $str .= $item->post . ' ';
        }

        foreach ($this->resume_skill as $item) {
            $str .= $item->skillname . ' ';
        }

        return rtrim($str);
    }

    public function getOtherAttribute()
    {
        $str = '';
        if (!empty($this->introduction)) {
            $str .= $this->introduction . ' ';
        }

        foreach ($this->resume_work_exp as $item) {
            if (!empty($item->content)) {
                $str .= $item->content . ' ';
            }
        }

        foreach ($this->resume_pro_exp as $item) {
            if (!empty($item->content)) {
                $str .= $item->content . ' ';
            }
        }

        foreach ($this->resume_edu_exp as $item) {
            if (!empty($item->speciality)) {
                $str .= $item->speciality . ' ';
            }
        }

        return rtrim($str);
    }
}
